package HomeView.service;

import HomeView.domain.House;

public class HomeService {

    private House[] house ;
    private  int houseNuber = 1;//记录当前有多少个房屋信息
    private int idCounter = 1;
    public  HomeService(int size){
        house = new House[size];
        house[0] = new House(1,"小明","123","北京",1000,"未出租");
    }

    public boolean add(House newHouse){

        if(houseNuber == house.length){
            System.out.println("数组数据已经满了，不能再添加了。。。。请添加size");
            return false;
        }

//        System.out.println("houseNuber======>>>>"+houseNuber);

        house[houseNuber++] = newHouse;
        houseNuber++;
        idCounter++;
        newHouse.setNober(idCounter);
        return true;

    }

    public boolean del(int delId){
        int index = -1;
        for (int i = 0; i <  houseNuber - 1; i++) {

//            System.out.println("delId============》》》》"+delId);
//            System.out.println("house[i].getNober()=======》》》"+house[i].getNober());

            if ( delId == house[i].getNober() ){
                index = i;
            }

        }

        if (index == -1) {
            return  false;
        }

        for (int i = index; i < houseNuber -1; i++) {
            house[i] = house[i+1];
        }
        house[--houseNuber] = null;
        return true;

    }

    public House[] list(){
//        System.out.println("houseNuber======>>>>"+houseNuber);
        return house;
    }


    public House[] select(int selectId){

        House[] newHouse = new House[houseNuber];
        for (int i = 0; i < houseNuber; i++) {
            if (selectId == house[i].getNober()){
                newHouse[0] = house[i];
            }
        }
        return newHouse;

    }

    public House findId(int selectId){
        for (int i = 0; i < houseNuber; i++) {
            if(selectId == house[i].getNober()){
                return house[i];
            }
        }
        return null;
    }

    public boolean update(int updataId, House newHouse){
        for (int i = 0; i < houseNuber; i++) {
            if(updataId == house[i].getNober()){
                house[i] = newHouse;
            }
        }
        return true;
    }


//    public House[] find(int updataId){
//        House[] newhouse = new House[houseNuber];
//
//        for (int i = 0; i < houseNuber; i++) {
//            if(updataId == house[i].getNober()){
//                newhouse[0] = house[i];
//            }
//        }
//        System.out.println("newhouse[0]===========>>>>>"+newhouse[0]);
//        return newhouse;
//    }




}
